﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using H2SDB;
using h2sbk_sharp.DoNetExt;

namespace h2sbk_sharp.Controllers
{
    public class UserController : ApiController
    {
        [HttpGet, HttpPost]
        public HttpResponseMessage all ()
        {
            using (h2sDataContext db = new h2sDataContext())
            {
                var result = db.user.Select(p => new
                {
                    id = p.id.ToString().PadLeft(6, '0'),
                    role = p.role == 0 ? "普通用户" : "管理员",
                    realname = p.realname,
                    phone = p.phone,
                    email = p.email,
                    active = p.active == 0 ? "禁用" : "启用"
                });
                return result.ToHttpResponseMessage();
            }
        }


        [HttpGet, HttpPost]
        public HttpResponseMessage add (string realname,
                                        string phone,
                                        string email)
        {
            string rtv = null;
            try
            {
                using (h2sDataContext db = new h2sDataContext())
                {
                    user user = new user();
                    user.password = "000000";
                    user.realname = realname;
                    user.phone = phone;
                    user.email = email;
                    user.active = 1;
                    user.role = 0;
                    db.user.InsertOnSubmit(user);
                    db.SubmitChanges();
                }
                rtv = "ok";  
            }
            catch (Exception e)
            {
                rtv = e.Message;
            }
            return rtv.ToHttpResponseMessage();
        }

        [HttpGet, HttpPost]
        public HttpResponseMessage del (int id)
        {
            string rtv = null;
            try
            {
                using (h2sDataContext db = new h2sDataContext())
                {
                    var user = db.user.Where(p => p.id == id).FirstOrDefault();
                    if (user != null)
                    {
                        db.user.DeleteOnSubmit(user);
                        db.SubmitChanges();
                        rtv = "ok";
                    }
                    else
                    {
                        rtv = "该用户不存在";
                    }
                }
            }
            catch (Exception e)
            {
                rtv = e.Message;
            }
            return rtv.ToHttpResponseMessage();
        }

        [HttpGet, HttpPost]
        public HttpResponseMessage update (int id,
                                           string realname,
                                           string phone,
                                           string email)
        {
            string rtv = null;
            try
            {
                using (h2sDataContext db = new h2sDataContext())
                {
                    var user = db.user.Where(p => p.id == id).FirstOrDefault();
                    if (user != null)
                    {
                        user.realname = realname;
                        user.phone = phone;
                        user.email = email;
                        db.SubmitChanges();
                        rtv = "ok";
                    }
                    else
                    {
                        rtv = "该用户不存在";
                    }
                }
            }
            catch (Exception e)
            {
                rtv = e.Message;
            }
            return rtv.ToHttpResponseMessage();
        }


        [HttpGet, HttpPost]
        public HttpResponseMessage active(int id, int status)
        {
            string rtv = null;
            try
            {
                using (h2sDataContext db = new h2sDataContext())
                {
                    var user = db.user.Where(p => p.id == id).FirstOrDefault();
                    if (user != null)
                    {
                        user.active = status;
                        db.SubmitChanges();
                        rtv = "ok";
                    }
                    else
                    {
                        rtv = "该用户不存在";
                    }
                }
            }
            catch (Exception e)
            {
                rtv = e.Message;
            }
            return rtv.ToHttpResponseMessage();
        }


        [HttpGet, HttpPost]
        public HttpResponseMessage reset(int id)
        {
            string rtv = null;
            try
            {
                using (h2sDataContext db = new h2sDataContext())
                {
                    var user = db.user.Where(p => p.id == id).FirstOrDefault();
                    if (user != null)
                    {
                        user.password = "000000";
                        db.SubmitChanges();
                        rtv = "ok";
                    }
                    else
                    {
                        rtv = "该用户不存在";
                    }
                }
            }
            catch (Exception e)
            {
                rtv = e.Message;
            }
            return rtv.ToHttpResponseMessage();
        }

        [HttpGet, HttpPost]
        public HttpResponseMessage pwd(int id, string password)
        {
            string rtv = null;
            try
            {
                using (h2sDataContext db = new h2sDataContext())
                {
                    var user = db.user.Where(p => p.id == id).FirstOrDefault();
                    if (user != null)
                    {
                        user.password = password;
                        db.SubmitChanges();
                        rtv = "ok";
                    }
                    else
                    {
                        rtv = "该用户不存在";
                    }
                }
            }
            catch (Exception e)
            {
                rtv = e.Message;
            }
            return rtv.ToHttpResponseMessage();
        }
    }
}
